import first.endtoend.Address
import first.endtoend.Admin
import first.endtoend.Agent
import first.endtoend.Aid
import first.endtoend.Beneficiary
import first.endtoend.Category
import first.endtoend.FairPricesShop
import first.endtoend.Family
import first.endtoend.Level
import first.endtoend.Locality
import first.endtoend.LocalityManager
import first.endtoend.Portfolio
import first.endtoend.PortfolioDetail
import first.endtoend.Product
import first.endtoend.RationCard
import first.endtoend.Role
import first.endtoend.Tag
import first.endtoend.UserRole
import first.endtoend.*

class BootStrap
{


    def init = { servletContext ->

        log.info("Begin insert data into database")

        Date startDate = new Date(System.currentTimeMillis())
        Date endDate = new Date(System.currentTimeMillis())
        endDate.setYear(2018)

        def aay = new Category(name: "AAY").save(flush: true)
        def annapoorna = new Category(name: "Annapoorna").save(flush: true)
        def bpl = new Category(name: "BPL").save(flush: true)

        log.info("Level insert")
        Level country = new Level(name: "Country").save(flush: true)
        Level region = new Level(name: "Region", parentLevel: country).save(flush: true)
        Level state = new Level(name: "State", parentLevel: region).save(flush: true)
        Level ut = new Level(name: "Country", parentLevel: region).save(flush: true)
        Level division = new Level(name: "Division", parentLevel: state).save(flush: true)
        Level district = new Level(name: "District", parentLevel: state).save(flush: true)
        Level departement = new Level(name: "Departement", parentLevel: state).save(flush: true)
        Level village = new Level(name: "Village", parentLevel: division).save(flush: true)
        Level city = new Level(name: "City", parentLevel: division).save(flush: true)

        //country.children = new ArrayList<Level>()
        //region.children = new ArrayList<Level>()
        //state.children = new ArrayList<Level>()
        //ut.children = new ArrayList<Level>()
        //division.children = new ArrayList<Level>()
        //district.children = new ArrayList<Level>()
        //village.children = new ArrayList<Level>()
        //city.children = new ArrayList<Level>()

//
        //  country.addToChildren(region).save(flush: true)
        //  region.addToChildren(state).save(flush: true)
        //  region.addToChildren(ut).save(flush: true)
        //  state.addToChildren(division).save(flush: true)
        //  state.addToChildren(district).save(flush: true)
        //  state.addToChildren(departement).save(flush: true)
        //  division.addToChildren(village).save(flush: true)
        //  division.addToChildren(city).save(flush: true)
//

        def photo1 = "benef1.jpg"
        def photo2 = "benef2.jpg"
        def photo3 = "benef3.jpg"
        def photo4 = "benef4.jpg"
        def photo5 = "benef5.jpg"
        def photo6 = "benef6.jpg"

        //TODO define a default level for user admin
        //def agentRole = SecRole.findByAuthority('ROLE_AGENT') ?: new SecRole(authority: 'ROLE_AGENT').save(failOnError: true)
        //def adminRole = SecRole.findByAuthority('ROLE_ADMIN') ?: new SecRole(authority: 'ROLE_ADMIN').save(failOnError: true)
        //
        //def adminUser = SecUser.findByUsername('admin') ?: new SecUse r(
        //       username: 'admin',
        //       password: springSecurityService.encodePassword('admin'),
        //       enabled: true).save(failOnError: true)

        // if (!adminUser.authorities.contains(adminRole)) {
        //  // SecUserSecRole.create adminUser, adminRole
        // }

        //def agentUser = SecUser.findByUsername('agent') ?: new SecUser(
        //      username: 'agent',
        //      password: springSecurityService.encodePassword('agent'),
        //      enabled: true).save(failOnError: true)

        //if (!adminUser.authorities.contains(agentRole)) {
        //  SecUserSecRole.create agentUser, agentRole
        //}
//
        log.info("Role insertion")
        def roleAdmin = Role.findByAuthority('ROLE_ADMIN') ?: new Role(authority: 'ROLE_ADMIN').save(failOnError: true)
        def roleAgent = Role.findByAuthority('ROLE_AGENT') ?: new Role(authority: 'ROLE_AGENT').save(failOnError: true)


        log.info("Locality insertion")
        Locality localityVillage = Locality.findByName("Bankey Bazar")
        LocalityManager aManager


        if (localityVillage)
        {
            aManager = new LocalityManager(locality: localityVillage, manager: userAdmin).save()
        }

        Locality locality1 = new Locality(level: country, dateCreated: startDate, lastUpdated: startDate, name: "INDIA").save(flush: true)
        Locality locality2 = new Locality(level: region, name: "North-India", dateCreated: startDate, lastUpdated: startDate, longitude: 150.5, latitude: 150.5).save(flush: true)
        Locality locality3 = new Locality(level: region, name: "South-India", dateCreated: startDate, lastUpdated: startDate, longitude: 150.5, latitude: 150.5).save(flush: true)
        Locality locality4 = new Locality(level: state, name: "National Capital Territory of Delhi", dateCreated: startDate, lastUpdated: startDate, longitude: 150.5, latitude: 150.5).save(flush: true)
        Locality locality5 = new Locality(level: city, name: "New Delhi", dateCreated: startDate, lastUpdated: startDate, longitude: 150.5, latitude: 150.5).save(flush: true)
        Locality locality6 = new Locality(level: state, name: "Karmataka", dateCreated: startDate, lastUpdated: startDate, longitude: 150.5, latitude: 150.5).save(flush: true)
        Locality locality7 = new Locality(level: state, name: "Tamil Nadu", dateCreated: startDate, lastUpdated: startDate, longitude: 150.5, latitude: 150.5).save(flush: true)
        Locality locality8 = new Locality(level: city, name: "Bangalore", dateCreated: startDate, lastUpdated: startDate, longitude: 150.5, latitude: 150.5).save(flush: true)
        Locality locality9 = new Locality(level: city, name: "Chennai", dateCreated: startDate, lastUpdated: startDate, longitude: 150.5, latitude: 150.5).save(flush: true)
        Locality locality10 = new Locality(level: city, name: "Nice", dateCreated: startDate, lastUpdated: startDate, longitude: 150.5, latitude: 150.5).save(flush: true)

        locality1.addToChildren(locality2).save(flush: true)
        locality1.addToChildren(locality3).save(flush: true)
        locality2.addToChildren(locality4).save(flush: true)
        locality4.addToChildren(locality5).save(flush: true)
        locality3.addToChildren(locality6).save(flush: true)
        locality3.addToChildren(locality7).save(flush: true)
        locality6.addToChildren(locality8).save(flush: true)
        locality7.addToChildren(locality9).save(flush: true)
        locality7.addToChildren(locality10).save(flush: true)

        log.info("Address insertion")
        def addressAdmin = new Address(locality: locality1).save(flush: true)
        def addressAgent = new Address(locality: locality2).save(flush: true)
        def addressAgent1 = new Address(numStreet: "800", locality: locality5, zipCode: "06000", localityName: locality5.name).save(flush: true)
        def addressAgent2 = new Address(numStreet: "1600", locality: locality8, zipCode: "69000", localityName: locality8.name).save(flush: true)
        def addressAgent3 = new Address(numStreet: "3200", locality: locality8, zipCode: "69000", localityName: locality8.name).save(flush: true)
        def addressAgent4 = new Address(numStreet: "6400", locality: locality9, zipCode: "42000", localityName: locality9.name).save(flush: true)

        Address address1 = new Address(numStreet: "350", locality: locality5, zipCode: "06000", localityName: locality5.name).save(flush: true)
        Address address2 = new Address(numStreet: "151", locality: locality5, zipCode: "06000", localityName: locality5.name).save(flush: true)
        Address address3 = new Address(numStreet: "651", locality: locality8, zipCode: "69000", localityName: locality8.name).save(flush: true)
        Address address4 = new Address(numStreet: "2400", locality: locality8, zipCode: "69000", localityName: locality8.name).save(flush: true)
        Address address5 = new Address(numStreet: "3150", locality: locality9, zipCode: "42000", localityName: locality9.name).save(flush: true)
        Address address6 = new Address(numStreet: "350", locality: locality10, zipCode: "06000", localityName: locality10.name).save(flush: true)



        log.info("User, Agent and Admin insertion")
        def levelInstance = Level.findByName("Country")
        def userAdmin = new Admin(username: 'admin', password: 'admin', phone: "0102030405", enabled: true, lastName: 'Administrator', firstName: 'Super', gender: 'Male', level: levelInstance, address: addressAdmin)
        def userAgent = new Agent(username: 'agent', password: 'agent', phone: "0102030405", enabled: true, lastName: 'agent', firstName: 'agent', gender: 'Male', address: addressAgent)
        def userAgent1 = new Agent(username: 'agent1', password: 'agent1', phone: "0102030405", enabled: true, lastName: 'Nicolas', firstName: 'Cordina', gender: 'Male', address: addressAgent1)
        def userAgent2 = new Agent(username: 'agent2', password: 'agent2', phone: "0102030405", enabled: true, lastName: 'Iman', firstName: 'El-Cherif', gender: 'Female', address: addressAgent2)
        def userAgent3 = new Agent(username: 'agent3', password: 'agent4', phone: "0102030405", enabled: true, lastName: 'Bluth', firstName: 'Micheal', gender: 'Male', address: addressAgent3)
        def userAgent4 = new Agent(username: 'agent4', password: 'agent4', phone: "0102030405", enabled: true, lastName: 'Lablaw', firstName: 'Bob', gender: 'Male', address: addressAgent4)

        userAdmin.save(flush: true)
        userAgent.save(flush: true)
        userAgent1.save(flush: true)
        userAgent2.save(flush: true)
        userAgent3.save(flush: true)
        userAgent4.save(flush: true)
        country.addToAdmins(userAdmin).save(flush: true)


        log.info("Fair Prices Shop insertion")
        FairPricesShop shopInstance = new FairPricesShop(fpsName: "Fair Shop 1", locality: locality5).save(flush: true)
        FairPricesShop shopInstance2 = new FairPricesShop(fpsName: "Fair Shop 2", locality: locality5).save(flush: true)
        FairPricesShop shopInstance3 = new FairPricesShop(fpsName: "Fair Shop 3", locality: locality8).save(flush: true)
        FairPricesShop shopInstance4 = new FairPricesShop(fpsName: "Fair Shop 4", locality: locality8).save(flush: true)
        FairPricesShop shopInstance5 = new FairPricesShop(fpsName: "Fair Shop 5", locality: locality9).save(flush: true)

        shopInstance.addToAgents(userAgent2).save(flush: true)
        shopInstance2.addToAgents(userAgent1).save(flush: true)
        shopInstance3.addToAgents(userAgent).save(flush: true)
        shopInstance4.addToAgents(userAgent3).save(flush: true)
        shopInstance5.addToAgents(userAgent4).save(flush: true)

        log.info("Tag insertion")
        Tag tag1 = new Tag(tagId: "famille1", type: "BENEFICIARY", dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        Tag tag2 = new Tag(tagId: "famille2", type: "BENEFICIARY", dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        Tag tag3 = new Tag(tagId: "famille3", type: "BENEFICIARY", dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        Tag tag4 = new Tag(tagId: "famille4", type: "BENEFICIARY", dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        Tag tag5 = new Tag(tagId: "famille5", type: "BENEFICIARY", dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        Tag tag6 = new Tag(tagId: "oil", type: "PRODUCT", dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        Tag tag7 = new Tag(tagId: "rice", type: "PRODUCT", dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        Tag tag8 = new Tag(tagId: "kerozene", type: "PRODUCT", dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        Tag tag9 = new Tag(tagId: "petit lu", type: "PRODUCT", dateCreated: startDate, lastUpdated: startDate).save(flush: true)

        log.info("Category insertion")
        Category category1 = new Category(name: "category1").save(flush: true)
        Category category2 = new Category(name: "TEST CATEGORY").save(flush: true)

        log.info("Product insertion")
        Product product1 = new Product(name: "Oil", tag: tag6, description: "Description oil", iconURL: "oil.jpg", unity: "L", pricePerUnit: 25.2, dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        Product product2 = new Product(name: "Kerozene", tag: tag7, description: "Description Kerozene", iconURL: "kerozene.jpg", unity: "L", pricePerUnit: 29.9, dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        Product product3 = new Product(name: "Rice", tag: tag8, description: "Descripion Rice", iconURL: "rice.jpg", unity: "Kg", pricePerUnit: 15.2, dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        Product product4 = new Product(name: "Petit Lu", tag: tag9, description: "Descripion Rice", iconURL: "lu.jpg", unity: "Kg", pricePerUnit: 15.2, dateCreated: startDate, lastUpdated: startDate).save(flush: true)

        log.info("Family insertion")
        Family family1 = new Family(familyName: "Family1", telephone: "0102030405", address: address6, fps: shopInstance, category: category1).save(flush: true)
        Family family2 = new Family(familyName: "Family2", telephone: "0102030405", address: address2, fps: shopInstance, category: category2).save(flush: true)
        Family family3 = new Family(familyName: "Family3", telephone: "0102030405", address: address3, fps: shopInstance, category: category1).save(flush: true)
        Family family4 = new Family(familyName: "Family4", telephone: "0102030405", address: address4, fps: shopInstance2, category: category1).save(flush: true)
        Family family5 = new Family(familyName: "Family5", telephone: "0102030405", address: address5, fps: shopInstance3, category: category1).save(flush: true)

        log.info("Beneficiary insertion")
        Beneficiary beneficiary1 = new Beneficiary(tataId: "Beneficiary1", family: family1, firstName: "Beneficiary1", lastName: "Beneficiary1", photoURL: photo1).save(flush: true)
        Beneficiary beneficiary2 = new Beneficiary(tataId: "Beneficiary2", family: family1, firstName: "Beneficiary2", lastName: "Beneficiary2", photoURL: photo2).save(flush: true)
        Beneficiary beneficiary3 = new Beneficiary(tataId: "Beneficiary3", family: family1, firstName: "Beneficiary3", lastName: "Beneficiary3", photoURL: photo3).save(flush: true)
        Beneficiary beneficiary4 = new Beneficiary(tataId: "Beneficiary4", family: family1, firstName: "Beneficiary4", lastName: "Beneficiary4", photoURL: photo4).save(flush: true)
        Beneficiary beneficiary5 = new Beneficiary(tataId: "Beneficiary5", family: family1, firstName: "Beneficiary5", lastName: "Beneficiary5", photoURL: photo5).save(flush: true)
        Beneficiary beneficiary6 = new Beneficiary(tataId: "Beneficiary6", family: family2, firstName: "Beneficiary6", lastName: "Beneficiary6", photoURL: photo6).save(flush: true)
        Beneficiary beneficiary7 = new Beneficiary(tataId: "Beneficiary7", family: family3, firstName: "Beneficiary7", lastName: "Beneficiary7", photoURL: photo1).save(flush: true)
        Beneficiary beneficiary8 = new Beneficiary(tataId: "Beneficiary8", family: family3, firstName: "Beneficiary8", lastName: "Beneficiary8", photoURL: photo2).save(flush: true)
        Beneficiary beneficiary9 = new Beneficiary(tataId: "Beneficiary9", family: family4, firstName: "Beneficiary9", lastName: "Beneficiary9", photoURL: photo3).save(flush: true)
        Beneficiary beneficiary10 = new Beneficiary(tataId: "Beneficiary10", family: family4, firstName: "Beneficiary10", lastName: "Beneficiary10", photoURL: photo4).save(flush: true)
        Beneficiary beneficiary11 = new Beneficiary(tataId: "Beneficiary11", family: family5, firstName: "Beneficiary11", lastName: "Beneficiary11", photoURL: photo5).save(flush: true)
        Beneficiary beneficiary12 = new Beneficiary(tataId: "Beneficiary12", family: family5, firstName: "Beneficiary12", lastName: "Beneficiary12", photoURL: photo6).save(flush: true)

        family1.beneficiarySelected = beneficiary1
        family2.beneficiarySelected = beneficiary6
        family3.beneficiarySelected = beneficiary7
        family4.beneficiarySelected = beneficiary9
        family5.beneficiarySelected = beneficiary11
        family1.save()
        family2.save()
        family3.save()
        family4.save()
        family5.save()

        log.info("Aids insertion")
        Aid aid1 = new Aid(startDate: startDate, endDate: endDate, category: category1).save(flush: true)
        Aid aid2 = new Aid(startDate: startDate, endDate: endDate, category: category2).save(flush: true)
        Aid aid3 = new Aid(startDate: startDate, endDate: endDate, category: category1).save(flush: true)

        log.info("Portfolio insertion")
        Portfolio pf1 = new Portfolio(family: family1, startDate: startDate, endDate: endDate, dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        Portfolio pf2 = new Portfolio(family: family2, startDate: startDate, endDate: endDate, dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        Portfolio pf3 = new Portfolio(family: family3, startDate: startDate, endDate: endDate, dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        Portfolio pf4 = new Portfolio(family: family4, startDate: startDate, endDate: endDate, dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        Portfolio pf5 = new Portfolio(family: family5, startDate: startDate, endDate: endDate, dateCreated: startDate, lastUpdated: startDate).save(flush: true)

        log.info("PortfolioDetail insertion")
        PortfolioDetail pfd10 = new PortfolioDetail(reduction: 5.5, quantity: 100, dateCreated: startDate, lastUpdated: startDate, product: product1, aid: aid1, portfolio: pf1).save(flush: true)
        PortfolioDetail pfd11 = new PortfolioDetail(reduction: 5.5, quantity: 200, dateCreated: startDate, lastUpdated: startDate, product: product2, aid: aid1, portfolio: pf1).save(flush: true)
        PortfolioDetail pfd12 = new PortfolioDetail(reduction: 5.5, quantity: 1, dateCreated: startDate, lastUpdated: startDate, product: product3, aid: aid1, portfolio: pf1).save(flush: true)
        PortfolioDetail pfd13 = new PortfolioDetail(reduction: 5.5, quantity: 1, dateCreated: startDate, lastUpdated: startDate, product: product4, aid: aid1, portfolio: pf1).save(flush: true)
        PortfolioDetail pfd20 = new PortfolioDetail(reduction: 15, quantity: 25, dateCreated: startDate, lastUpdated: startDate, product: product2, aid: aid1, portfolio: pf2).save(flush: true)
        PortfolioDetail pfd21 = new PortfolioDetail(reduction: 15, quantity: 25, dateCreated: startDate, lastUpdated: startDate, product: product3, aid: aid1, portfolio: pf2).save(flush: true)
        PortfolioDetail pfd22 = new PortfolioDetail(reduction: 15, quantity: 25, dateCreated: startDate, lastUpdated: startDate, product: product2, aid: aid1, portfolio: pf2).save(flush: true)
        PortfolioDetail pfd30 = new PortfolioDetail(reduction: 45, quantity: 684, dateCreated: startDate, lastUpdated: startDate, product: product3, aid: aid2, portfolio: pf3).save(flush: true)
        PortfolioDetail pfd40 = new PortfolioDetail(reduction: 50, quantity: 184, dateCreated: startDate, lastUpdated: startDate, product: product4, aid: aid3, portfolio: pf4).save(flush: true)
        PortfolioDetail pfd50 = new PortfolioDetail(reduction: 0.01, quantity: 106, dateCreated: startDate, lastUpdated: startDate, product: product1, aid: aid2, portfolio: pf5).save(flush: true)
        PortfolioDetail pfd51 = new PortfolioDetail(reduction: 0.15, quantity: 106, dateCreated: startDate, lastUpdated: startDate, product: product2, aid: aid2, portfolio: pf5).save(flush: true)

        log.info("RationCard insertion")
        RationCard rc1 = new RationCard(validity: endDate, family: family1, tag: tag1, dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        RationCard rc2 = new RationCard(validity: endDate, family: family2, tag: tag2, dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        RationCard rc3 = new RationCard(validity: endDate, family: family3, tag: tag3, dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        RationCard rc4 = new RationCard(validity: endDate, family: family4, tag: tag4, dateCreated: startDate, lastUpdated: startDate).save(flush: true)
        RationCard rc5 = new RationCard(validity: endDate, family: family5, tag: tag5, dateCreated: startDate, lastUpdated: startDate).save(flush: true)

        log.info("Userole creation")
        UserRole.create(userAdmin, roleAdmin)
        userAdmin.localitieManagers = new ArrayList<LocalityManager>()
        userAdmin.save()

        UserRole.create(userAgent, roleAgent)
        UserRole.create(userAgent1, roleAgent)
        UserRole.create(userAgent2, roleAgent)
        UserRole.create(userAgent3, roleAgent)
        UserRole.create(userAgent4, roleAgent)

        userAgent.save(flush: true)
        userAgent1.save(flush: true)
        userAgent2.save(flush: true)
        userAgent3.save(flush: true)
        userAgent4.save(flush: true)

        log.info("Done insert data into database")
        log.info("Insert Certificate into java")
        System.setProperty("javax.net.ssl.trustStore","C:\\Users\\ncordina\\IdeaProjects\\first\\Docs\\TSM\\SOUAPUI\\Security settings\\Truststores\\tester.p12")
        System.setProperty("javax.net.ssl.trustStorePassword","password3")
        System.setProperty("javax.net.ssl.keyStore","C:\\Users\\ncordina\\IdeaProjects\\first\\Docs\\TSM\\SOUAPUI\\Security settings\\Keystores\\tester.p12")
        System.setProperty("javax.net.ssl.keyStorePassword","password3")
        System.setProperty("javax.net.ssl.trustStore","C:\\Users\\ncordina\\IdeaProjects\\first\\Docs\\TSM\\SOUAPUI\\Security settings\\Truststores\\tester_wo_sp.p12")
        System.setProperty("javax.net.ssl.trustStorePassword","password3")
        System.setProperty("javax.net.ssl.keyStore","C:\\Users\\ncordina\\IdeaProjects\\first\\Docs\\TSM\\SOUAPUI\\Security settings\\Keystores\\tester_wo_sp.p12")
        System.setProperty("javax.net.ssl.keyStorePassword","password3")



    }
    def destroy = {
    }
}
